package com.winit.deploy.handler;

import com.winit.common.util.SpringContextHolder;
import com.winit.core.mq.exchange.MessageExchange;
import com.winit.core.mq.message.Message;
import com.winit.deploy.bean.AreaStatus;
import com.winit.deploy.util.DeployFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangjian on 16/8/17.
 * 接受任务结束状态
 * 区域内任务完成后更新区域状态
 */
public class TaskStatusHandler {

    private TaskStatusHandler(){}

    private static final TaskStatusHandler TASK_STATUS_HANDLER = new TaskStatusHandler();

    public static TaskStatusHandler getInstance(){
        return TASK_STATUS_HANDLER;
    }

    private static final Logger logger = LoggerFactory.getLogger(TaskStatusHandler.class);

    private static Map<String,Map<String,String>> areaTaskCountMap = new HashMap<>();

    public void addArea(String envName,String areaName,String queueName,String taskId) throws Exception{
        String key = envName+"_"+areaName;

        if(!this.areaTaskCountMap.containsKey(key)){
            AreaStatus areaStatus = DeployFileUtil.getAreaStatus(envName,areaName);
            areaStatus.setStatus("running");
            DeployFileUtil.saveAreaStatus(envName,areaName,areaStatus);
            pushAreaStatus(envName,areaName,queueName,areaStatus);
        }

        Map<String,String> map = new HashMap<>();
        if(this.areaTaskCountMap.containsKey(key)){
            map = this.areaTaskCountMap.get(key);
        }
        map.put(taskId,"1");//任务执行中

        this.areaTaskCountMap.put(key,map);
        logger.info("添加任务成功.envName="+envName+",areaName="+areaName);
    }

    public void handleAreaTaskStatus(String envName,String areaName,String taskId,String queueName){
        String key = envName+"_"+areaName;
        if(!this.areaTaskCountMap.containsKey(key)){
            logger.info("当前区域没有任务在执行.envName="+envName+",areaName="+areaName);
            return;
        }
        Map<String,String> map = this.areaTaskCountMap.get(key);
        if(map == null){
            logger.info("当前区域没有任务在执行.envName="+envName+",areaName="+areaName);
            return;
        }
        map.put(taskId,"0");//任务已执行完成
        Integer count = 0;
        for(String id:map.keySet()){
            if("1".equals(map.get(id))){
                count = 1;
                break;
            }
        }
        if(count != 0){
            logger.info("当前区域仍有任务在执行,不修改状态.envName="+envName+",areaName="+areaName);
        }else {
            this.areaTaskCountMap.remove(key);
            AreaStatus areaStatus = DeployFileUtil.getAreaStatus(envName,areaName);
            areaStatus.setStatus("ready");
            DeployFileUtil.saveAreaStatus(envName,areaName,areaStatus);
            pushAreaStatus(envName,areaName,queueName,areaStatus);
            logger.info("当前区域任务已全部完成,修改状态.envName="+envName+",areaName="+areaName);
        }
    }

    public void cleanAreaTask(String envName,String areaName,String queueName){
        String key = envName+"_"+areaName;
        this.areaTaskCountMap.remove(key);
        AreaStatus areaStatus = DeployFileUtil.getAreaStatus(envName,areaName);
        areaStatus.setStatus("ready");
        DeployFileUtil.saveAreaStatus(envName,areaName,areaStatus);
        pushAreaStatus(envName,areaName,queueName,areaStatus);
    }

    private void pushAreaStatus(String envName,String areaName,String queueName,AreaStatus areaStatus){
        Map head = new HashMap();
        head.put("queueName",queueName);
        head.put("envName",envName);
        head.put("areaName",areaName);
        head.put("type","areaStatus");

        Message<AreaStatus> message = new Message<>(head,areaStatus);
        MessageExchange messageExchange = SpringContextHolder.getBean(MessageExchange.class);
        messageExchange.sendMessage(queueName,message);
    }

}
